//
//  BHHomeHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/8.
//

import UIKit
@objc protocol BHHomeHeaderViewDelegate: NSObjectProtocol {
    func homeSecDidChangeToIndex(index: Int)
    func homeItemGroupDidSelectIndex(index: Int)
}

class BHHomeHeaderView: UIView {

    @IBOutlet weak var bannerContentView: UIView!
    
    var delegate: BHHomeHeaderViewDelegate?
    
    var banner: [BHCommonBannerModel] = [] {
        didSet {
            self.bannerView.sourceArray = banner
        }
    }
    
    
    static func loadFromXib() -> BHHomeHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHHomeHeaderView", owner: nil, options: nil)
        let view: BHHomeHeaderView = viewArray![0] as! BHHomeHeaderView
        return view
    }
    
    private let homeTapArray: [CZTopSegMentItemModel] = BHHomeManager.loadHomeTapArray
    
    lazy var bannerView: BHCycleView = {
        let view = BHCycleView.init()
        view.backgroundColor = .color_f8f8f8
        view.collectionView.backgroundColor = .color_f8f8f8
        view.scrollDirection = .horizontal
        view.timerValue = 8
        view.leadingAndTralingSpace = 0
        view.lineSpace = 0
        view.dataSource = self
        view.delegate = self
        view.addLayerCorner(10, 0, .white)
        return view
    }()
    
    lazy var pageCycleView: CZPageView = {
        let view = CZPageView.init(frame: CGRect(x: 0, y: CGFloat(130).radius+CGFloat(115), width: UIScreen.width, height: 50))
        view.backgroundColor = .color_f8f8f8
        view.delegate = self
        let config = CZPageConfig.init()
        config.showLine = true
        config.lineSize = CGSize(width: 20, height: 4)
        config.lineRadius = 2
        config.lineColor = .color_34CE98
        config.nomalFont = .systemFont(ofSize: 14, weight: .medium)
        config.selectFont = .systemFont(ofSize: 16, weight: .bold)
        config.nomalColor = .color_666666
        config.selectColor = .color_34CE98
        config.divideScreenWidth = true
//        config.itemSpace = 20
//        config.leadingSpace = 25
//        config.tralingSpace = 5
        
        view.config = config
        return view
    }()
    
    override func awakeFromNib() {
        
        self.addSubview(self.pageCycleView)

        self.pageCycleView.itemArray = self.homeTapArray
        self.addSubview(self.bannerView)
        self.bannerView.mas_makeConstraints { (make) in
            make?.left.right()?.bottom()?.top()?.equalTo()(self.bannerContentView)
        }
    }

    
    static var headerHeight: CGFloat {
        get {
            let bannerHeight = CGFloat(130).radius
            let realHeight = CGFloat(115+50)
            return bannerHeight+realHeight
        }
    }
    
    // 左到右  100-> 103
    @IBAction func homeItemAct(_ sender: UITapGestureRecognizer) {
        let tag = sender.view!.tag-100
        self.delegate?.homeItemGroupDidSelectIndex(index: tag)
    }
}


extension BHHomeHeaderView: CZPageViewDelegate {
    func pageViewSelectdIndex(_ index: Int, with model: CZTopSegMentItemModel, with pageView: CZPageView) {
        self.delegate?.homeSecDidChangeToIndex(index: index)
    }
}
extension BHHomeHeaderView: BHCycleViewDataSource,BHCycleViewDelegate {
    func cellForRow(_ indexPath: IndexPath, _ collectionView: UICollectionView,_ cycleView: UIView) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCommonBannerImageCell", for: indexPath) as! BHCommonBannerImageCell
        let model = self.banner[indexPath.item%self.banner.count]
        cell.bannerImageUrl = model.image_url
        cell.backgroundColor = .clear
        return cell
    }
    func sizeForItem(_ indexPath: IndexPath,_ cycleView: UIView) -> CGSize {
        return CGSize(width: UIScreen.width-32, height: CGFloat(138).radius)
    }
    
    func numberOfItem(_ cycleView: UIView) -> Int {
        return self.banner.count
    }
    func didSelectItem(_ index: Int,_ cycleView: UIView) {
        let model = self.banner[index]
        BHRouter.shared.bh_bannerClick(model: model)
//        self.bannerDidSelectIndexBlock?(index,model)
    }
}
